<?php


namespace app\admin\controller\pointgoods;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;

class Pointgorder extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }
    public function index(){
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                $where[]=[];
            }
            $_list = Db::name('points_goods_order')
                ->alias('mf')
                ->join('member m', 'mf.member_id=m.id')
                ->field('mf.*,m.nickname,m.mobile,m.avatarUrl')
                ->where($where)
                ->order('create_time desc')
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k=>$v){
                $_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                $goods_info=Db::name('points_goods')->find($v['goods_id']);
                $_list[$k]['goods_name']=$goods_info['name'];
            }
            $total = Db::name('points_goods_order')
                ->alias('mf')
                ->join('member m', 'mf.member_id=m.id')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }
    public function send()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['status']=2;
            $data['row']['express_time']=time();
            $num = Db::name('points_goods_order')->update($data['row']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        //获取快递公司
        $express = Db::name('express')
            ->where(['is_del' => 0,'status' => 1])
            ->order('sort desc')
            ->select();
        $data = Db::name('points_goods_order')->find($id);
        $this->assign([
                'data'=>$data,
                'express'=>$express
            ]
        );
        return $this->fetch('');
    }
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['status']=2;
            $num = Db::name('points_goods_order')->update($data['row']);
            $this->success('操作成功！');
        }

        $id = $this->request->param('id/d', 0);
        $data = Db::name('points_goods_order')->find($id);
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        $this->assign([
                'data'=>$data,
            ]
        );
        return $this->fetch('');
    }

}